# = Active Background
# 
# Something about active background
# 
# == MiddleMan
# 
# The client program to communicate with the server
# 
# MiddleMan allows you to connect to the ActiveBackground::Server
# to push jobs and retrieve jobs. The jobs are managed by
# ActiveBackground::Manager.
# 
# == Client
# 
# The ActiveBackground::Client is responsible for initializing connection, managing
# the connection and handeling local command cache if the server starts again. The
# client is extended by ActiveBackground::MiddleMan that is responsible for communication
# between client and the local code (and Handlers).
# 
# It is preferred not to use ActiveBackground::Client directly but to use MiddleMan,
# but if you are not happy with ActiveBackground::MiddleMan extend
# ActiveBackground::Client. You can use the IRB console to communicate with your server.
# 
# === Example
#  
#   ./script/console
#   
# Will open an IRB console with access to ActiveBackground.
# 
# == Server
# 
# The server module is responsible for installing (as service), running,
# stopping, detaching, and terminating the DRb server. Use one of the 
# supplied rake tasks to start the server like:
# 
#   rake ActiveBackground::server
# 
# But you can also use one of the following:
# 
#   rake ActiveBackground::start
# 
# You can pass parameters like -d to detach in unix and install/run as service
# in windows. This allows the proces to run in the background and but you can
# still terminate it by using the rake task:
# 
#   rake ActiveBackground::stop
#   
# It uses the PID stored in temp/ActiveBackground.pid to terminate
# the task.
# 
module ActiveBackground
  class Base
    def initialize
    end
    
    def setup
      @config = ActiveBackground::Configuration.instance
      @logger = @config.logger
    end
    
    def error_message(exception)
    end
  end
  
  class Initializer
    def self.init
      ActiveBackground::Configuration.instance.init
      require ActiveBackground::Configuration.instance.root + "/config/active_background_config.rb"
      return ActiveBackground::Configuration.instance
    end
    
    def self.run
      @config=ActiveBackground::Configuration.instance
      @config.process
      @logger = @config.logger
      @logger.info('Initializer: Configuration has been processed')
      @logger.debug("Initializer: Configuration.instance - #{@config.inspect}")
      # Database
      ActiveBackground::Database.instance.setup
      
      @middle_man=ActiveBackground::MiddleManProxy.instance
      context = ActiveBackground::MiddleMan.instance
      context.setup
      @middle_man.context=context
      
      @config.handler.get_all.each do |name, handler|
        #TODO: log...
        @middle_man.register(handler)
      end
    end
    
    def self.configure
      yield ActiveBackground::Configuration.instance
      ActiveBackground::Configuration.instance.process if ActiveBackground::Configuration.instance.runned
    end
    
    def self.environment(environment)
      ActiveBackground::Configuration.instance.environment=environment
      require File.dirname(__FILE__) + "/../config/environment/#{environment.to_s}.rb"
    end
  end
end

class Object
  module InstanceExecHelper; end
  include InstanceExecHelper
  def instance_exec(*args, &block) # !> method redefined; discarding old instance_exec
    mname = "__instance_exec_#{Thread.current.object_id.abs}_#{object_id.abs}"
    InstanceExecHelper.module_eval{ define_method(mname, &block) }
    begin
      ret = send(mname, *args)      
    ensure
      InstanceExecHelper.module_eval{ undef_method(mname) } rescue nil
    end
    ret
  end
end